/* 
 * AUTHOR: Kevin Lam
 */

package com.apps.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.apps.ubc.cc.model.SaveonBookModel;

public class BookSearchUtils {
	
	public static final String KEYWORD_SPLIT_REGEX = "[^a-zA-Z0-9]";
	
	public static final String AUTHOR_LIST_SPLIT_REGEX = "/";
	
	public static final String EDITION_KEYWORD_REGEX = "[edn|edition|e]";
	
	public static final String ONE_REGEX = "one|1(st)?|first";
	
	public static final String TWO_REGEX = "two|2(nd)?|second";
	
	public static final String THREE_REGEX = "three|3(rd)?|third";
	
	public static final String FOUR_REGEX = "fo(u)?r(th)?|4(th)?";
	
	public static final String FIVE_REGEX = "five|5(th)?|fifth";
	
	public static final String SIX_REGEX = "six(th)?|6(th)?";
	
	public static final String SEVEN_REGEX = "seven(th)?|7(th)?";
	
	public static final String EIGHT_REGEX = "eight(th)?|8(th)?";
	
	public static final String NINE_REGEX = "nin(e)?(th)?|9(th)?";
	
	

	public static void main(String[] args) {
		// List<String> ls =
		// findKeywords("Solutions Manual for Discrete Mathematics 4Th Edn W/ Applications 4/E");
		//List<String> ls = parseAuthors("COURSESMART / EPP / 114");
		
//		for (Iterator i = ls.iterator(); i.hasNext();)
//			System.out.println(i.next());
	}

	public BookSearchUtils() {

	}

	public static List<String> findKeywords(String in) {
		in = in.replaceAll("\\(.+\\)", "");
		String[] temp = in.split("[^a-zA-Z0-9]");
		List<String> keywords = new ArrayList<String>();
		for (int i = 0; i < temp.length; i++) {
			if (temp[i].length() > 3)
				keywords.add(temp[i]);
		}
		return keywords;
	}
	
	public static int countKeywordMatches(List<String> keywords, String value){
		int matches = 0;
		for(Iterator i = keywords.iterator();i.hasNext();){
			if(value.contains(((String)i.next())))
				matches++;
		}
		return matches;
	}

	public static List<String> parseAuthors(String in) {
		String[] temp = in.split("/");
		String[] result = new String[temp.length];
		for (int i = 0; i < result.length; i++)
			result[i] = temp[i].trim();
		List<String> authors = Arrays.asList(result);
		return authors;
	}

	public static String parseEdition(String in){
		String[] temp = in.split("[^a-zA-Z0-9]");
		String result = "";
		for(int i = 0; i<temp.length; i++){
			if (temp[i].toLowerCase().matches("(edn|edition|e)") && i > 0)
				result = temp[i-1];
			else if(temp.length == 1){
				result =temp[i];
			}
		}
		if (result.toLowerCase().matches(ONE_REGEX)) {
			result = "1";
		}
		if (result.toLowerCase().matches(TWO_REGEX)) {
			result = "2";
		}
		if (result.toLowerCase().matches(THREE_REGEX)) {
			result = "3";
		}
		if (result.toLowerCase().matches(FOUR_REGEX)) {
			result = "4";
		}
		if (result.toLowerCase().matches(FIVE_REGEX)) {
			result = "5";
		}
		if (result.toLowerCase().matches(SIX_REGEX)) {
			result = "6";
		}
		if (result.toLowerCase().matches(SEVEN_REGEX)) {
			result = "7";
		}
		if (result.toLowerCase().matches(EIGHT_REGEX)) {
			result = "8";
		}
		if (result.toLowerCase().matches(NINE_REGEX)) {
			result = "9";
		}
		return result;
	}
	
	public static String parseSearchableBookName(List<SaveonBookModel> books, List<String> keywords){
		String bestSearchable = "";
		int highestMatches = 0;
		for(Iterator i = books.iterator(); i.hasNext();) {
			String bookName = ((SaveonBookModel)i.next()).getTitle();
			int matchCount = countKeywordMatches(keywords,bookName);
			if(highestMatches <= matchCount) {
				highestMatches = matchCount;
				bestSearchable = bookName;
			}
		}
		return bestSearchable;
		
	}

}
